﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Money.Interface
{
    public partial class Settings : Form
    {
        private static Categories _categories;

        public Categories Categories
        {
            get { return _categories; }
            set
            {
                _categories = value;
//                _parentIdColumn.DataPropertyName = "ParentId";
                // TODO we cannot bind to the source table direct as it crashes when selecting the parent, need to look at why? Using ToList creates a new object and breaks the link.
//                _parentIdColumn.DataSource = _categories.GetCategoryGroups().ToList();
//                _parentIdColumn.DisplayMember = "Name";
//                _parentIdColumn.ValueMember = "Id";
//                _parentIdColumn.ValueType = typeof(int);

                _categoryGrid.AutoGenerateColumns = false;

                _categoryGrid.DataSource = _categories;
            }
        }

        public Settings()
        {
            InitializeComponent();
        }
 
        private void categoryGrid_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void categoryGrid_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
            {
                DataGridView grid = sender as DataGridView;

                DataGridViewColumn gridColumn = grid.Columns[e.ColumnIndex];
                Category rowCategory = grid.Rows[e.RowIndex].DataBoundItem as Category;

                switch (gridColumn.HeaderText)
                {
                    case "Parent":
                        
                        //var parentList = from category in _categories where category.Id != rowCategory.Id orderby category.Name ascending select category;
                        
                        DataGridViewComboBoxCell cell = (DataGridViewComboBoxCell)(_categoryGrid.Rows[e.RowIndex].Cells["clmParentId"]);
                        cell.DataSource = _categories.GetCategoryGroups().ToList();
                        break;
                }
            }
        }

        private void categoryGrid_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right && e.RowIndex >= 0)
            {
                DataGridView grid = sender as DataGridView;

                if (grid != null)
                {
                    //grid.ClearSelection();
                    //grid.Rows[e.RowIndex].Selected = true;

                    Category category = grid.Rows[e.RowIndex].DataBoundItem as Category;
                    _deleteCategoryMenuItem.Tag = category;
                }

                _categoryGridContextMenu.Show(System.Windows.Forms.Cursor.Position);
            }
        }

        private void deleteCategory_Click(object sender, EventArgs e)
        {

        }

        private void newCategory_Click(object sender, EventArgs e)
        {
            NewCategoryForm newCategoryForm = new NewCategoryForm(_categories);

            if (newCategoryForm.ShowDialog() == DialogResult.OK)
            {
                _categories.Add(newCategoryForm.Category);
                _categoryGrid.DataSource = null;
                _categoryGrid.DataSource = _categories;
            }
        }
    }
}
